import * as React from 'react'

export class PolygonUtil extends React.Component{

    // for the diamond shape
    static createPolygon (props)  {
        if(!props || !props.points){
            throw new Error("The props or points couldn't not be null if you want to create a real polygon!")
        }

        const {color, stroke} = props
        return (
                <polygon
                    vectorEffect="non-scaling-stroke"
                    fill={color}
                    stroke={stroke}
                    {...props}
                />
        )
    }

    static createDiamondPolygon (polygonProps)  {
        PolygonUtil.validate(polygonProps)

        const{size} = polygonProps
        if(!size){
            throw new Error("The size of of the props should not be null!")
        }

        polygonProps.points = PolygonUtil.getDiamondPolygonPoints(size, 2)

        return PolygonUtil.createPolygon(polygonProps)
    }

    static getDiamondPolygonPoints (size, strokeMiterlimit) {
        
        const points = "" + strokeMiterlimit  + ","  + (size/2) + " " +
                            (size / 2) + "," + strokeMiterlimit + " " +
                            (size - strokeMiterlimit) + "," + (size / 2) + " " +
                            (size / 2) + `,` + (size - strokeMiterlimit)

        return points
    }

// rounded rectangle

    static createRoundedRectangle (polygonProps){
        PolygonUtil.validate(polygonProps)

        return PolygonUtil.getRoundedRectangleShape(polygonProps.width, polygonProps.height, polygonProps.strokeWidth, polygonProps.color, polygonProps.stroke)
    }

    static getRoundedRectangleShape (width, height, strokeWidth, color, stroke) {
        return <rect vectorEffect="non-scaling-stroke" x={strokeWidth} y={strokeWidth} width={width - strokeWidth * 2} height={height - strokeWidth * 2} rx="10" ry="10" fill={color} strokeWidth={strokeWidth} stroke={stroke}></rect>
    }

    //for the circle shape
    static createCircle (polygonProps){
        PolygonUtil.validate(polygonProps)

        return PolygonUtil.getCircleShape(polygonProps.size, polygonProps.strokeWidth, polygonProps.color, polygonProps.stroke)
    }

    static getCircleShape (size, strokeWidth, color, stroke) {
        return <circle vectorEffect="non-scaling-stroke" cx={size/2} cy={size/2} r={size/2 - strokeWidth}fill={color} strokeWidth={strokeWidth} stroke={stroke}></circle>
    }


    static validate (polygonProps){
        if(!polygonProps ){
            throw new Error("The polygonProps couldn't not be null if you want to create a real polygon!")
        }
    }

}
